/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Oct 8, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.collection;

import java.util.*;

public class CollectionUtils {
    public static final boolean DEBUG = false;

   /**
    * Assume the Hastables contain only
    * @param frm
    * @param to
    */
    public static void copy(Hashtable frm, Hashtable to)  {
    }
    /*******************************************************************************************************
     *                array
     *******************************************************************************************************/
     public static int[] convertToIntArray(Set set){
        int[] intA;
        if (set.size() <=0){
            intA=new int[0];
        }
        else{
           intA=new int[set.size() ];
            Object[] tmp=set.toArray() ;
            for (int i=0;i<intA.length ;i++){
                intA[i]=((Integer)tmp[i]).intValue() ;
            }
        }
          return intA;
    }
    public static HashSet convertArrayToSet(int[] array){
        HashSet set=new HashSet();
        for (int i=0;i<array.length ;i++){
            Integer e=new Integer(array[i]);
            set.add(e);
        }
        return set;
    }
    public static void convertArrayToSet(HashSet set,int[] array){
        //HashSet set=new HashSet();
        for (int i=0;i<array.length ;i++){
            Integer e=new Integer(array[i]);
            set.add(e);
        }
        //return set;
    }
    public static List convertArrayToList(int[] array){
        ArrayList  list=new ArrayList();
        for (int i=0;i<array.length ;i++){
            Integer e=new Integer(array[i]);
            list.add(e);
        }
        return list;
    }
    public static List convertArrayToList(String [] array){
        ArrayList  list=new ArrayList();
        for (int i=0;i<array.length ;i++){
            //String  s=array[i];
            list.add(array[i]);
        }
        return list;
    }
    public static Object[] convertToArray(Set set){
        if(set==null){
            throw new NullPointerException("Can't convert null set");
        }
        Object[] ss=new Object[set.size() ];
        int i=0;
        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
            Object s =  iterator.next();
            ss[i++]=s;
        }
        return ss;
    }
    public static String[] convertToStringArray(Set set){
        if(set==null){
            throw new NullPointerException("Can't convert null set");
        }
        String[] ss=new String[set.size() ];
        int i=0;
        for (Iterator iterator = set.iterator(); iterator.hasNext();) {
            String s = (String) iterator.next();
            ss[i++]=s;
        }
        return ss;
    }
    public static String[] convertToStringArray(List list){
        if(list==null){
            throw new NullPointerException("Can't convert null list");
        }
        String[] ss=new String[list.size() ];
        int i=0;
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            String s = (String) iterator.next();
            ss[i++]=s;
        }
        return ss;
    }


     /*******************************************************************************************************
     *                Vector
     *******************************************************************************************************/

    public  static Vector convertToVector(Object[] anArray) {
        if (anArray == null) {
            return null;
        }
        Vector v = new Vector(anArray.length);
        for (int i=0; i < anArray.length; i++) {
            v.add(anArray[i]);
        }
        return v;
    }
    /**
     * Returns a vector that contains the same objects as the array.
     * @param anArray  the array to be converted
     * @return  the new vector; if <code>anArray</code> is <code>null</code>,
     *				returns <code>null</code>
     */
    public  static Vector convertFloatToVector(float[] anArray) {
        if (anArray == null||anArray.length <=0) {
            return null;
        }
        Vector v = new Vector(anArray.length);
        for (int i=0; i < anArray.length; i++) {
            v.add(new Float(anArray[i]));
        }
        return v;
    }
    public  static Vector convertDoubleToVector(double[] anArray) {
        if (anArray == null||anArray.length <=0) {
            return null;
        }
        Vector v = new Vector(anArray.length);
        for (int i=0; i < anArray.length; i++) {
            v.add(new Float(anArray[i]));
        }
        return v;
    }
    /**
     * assume v contain Integer or Double or Float or Long
     * @param v
     * @return
     */
    public static float[] convertVectorToFloat(Vector v){
        Object[] os=v.toArray();
        float[] result=new float[os.length ];
        for (int i=0;i<os.length ;i++){
            Object o=os[i];
            result[i]=floatValue(o);

        }
        return result;
    }
    /**
     * assume v contain Integer or Double or Float or Long
     * @param v
     * @return
     */
    public static double[] convertVectorToDouble(Vector v){
        Object[] os=v.toArray();
        double[] result=new double[os.length ];
        for (int i=0;i<os.length ;i++){
            Object o=os[i];
            result[i]=floatValue(o);

        }
        return result;
    }

    public static float floatValue(Object o) {
        float f=0;
        if(o instanceof Integer){
            f=((Integer)o).floatValue() ;
        }
        else if(o instanceof Double){
            f=((Double)o).floatValue() ;
        }
        else if(o instanceof Float){
            f=((Float)o).floatValue() ;
        }
        else{
            f=((Long)o).floatValue() ;
        }
        return f;
    }


}
